package com.sixmac.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sixmac.model.Comment;
import com.sixmac.service.CommentService;

import static com.sixmac.core.constant.ConstCurd.KEY_ADD;
import static com.sixmac.core.constant.ConstCurd.KEY_MAIN;

/**
 * CommentController
 * 2018-06-10 14:51:35
 *
 * @author Blade
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {
    private static String CODE = "comment";
    private static String PREFIX = "t_comment";
    private static String LIST_SOURCE = "comment.list";
    private static String BASE_PATH = "/platform/comment/";

    @Autowired
    CommentService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        mm.put("aid", getParameter("aid"));
        mm.put("pid", getParameter("pid"));
        return BASE_PATH + "comment.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "comment_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        Comment comment = service.findById(id);
        mm.put("model", JsonKit.toJson(comment));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "comment_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        Comment comment = service.findById(id);
        mm.put("model", JsonKit.toJson(comment));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "comment_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Integer aid = getParameterToInt("aid", 0);
        Integer pid = getParameterToInt("pid", 0);
        String sql = "select c.*,u.nickname ,t.title,(select count(*) from t_comment where pid = c.id)as reply from  t_comment c left join t_user u on u.id = c.userId inner join t_content t on c.articleId = t.id where 1= 1 and c.articleId = " + aid + " and c.pid = " + pid;
        Object grid = paginate(sql);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        Comment comment = mapping(PREFIX, Comment.class);
        boolean temp = service.save(comment);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        Comment comment = mapping(PREFIX, Comment.class);
        boolean temp = service.update(comment);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }
}
